package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.dao.ChatDao;
import com.hlingsoft.kkh.entity.AppUser;
import com.hlingsoft.kkh.entity.Chat;
import com.hlingsoft.kkh.entity.Comment;
import com.hlingsoft.kkh.entity.MessageStatus;
import com.hlingsoft.kkh.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/6/17.
 */
@Controller
@RequestMapping("v20/chat")
public class ChatAction extends BaseAction {

    @Autowired
    private ChatDao chatDao;


    private static final int rows=10;


    /**
     * 添加私聊
     * @param myId
     * @param toId
     * @param content
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> like(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "toId")  long toId,
            @RequestParam(value = "content")String content,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (request.getMethod()=="GET"){
                content= Common.encodeStr(content);
            }
            AppUser toUser = userDao.getUserById(toId);
            if (toUser!=null){
                Chat chat = new Chat();
                Date now = new Date();
                chat.setMyId(myId);
                AppUser myUser = userDao.getUserById(myId);
                chat.setMyName(myUser.getName());
                chat.setMyImageHead(myUser.getHeadImage());
                chat.setToId(toId);
                chat.setToName(toUser.getName());
                chat.setToImageHead(toUser.getHeadImage());
                chat.setContent(content);
                chat.setCreateTime(now);
                chat.setUpdateTime(now);
                chatDao.addChat(chat);

                MessageStatus messageStatus = new MessageStatus();
                messageStatus.setAppUserId(toId);
                messageStatus.setType(1);
                messageStatus.setMessageId(chat.getId());
                messageStatusDao.addMessageStatus(messageStatus);

                resultMap.put("chat",chat);
                resultMap.put("result",true);
            }else {
                resultMap.put("msg","抱歉,发送用户已被禁用!");
                resultMap.put("result",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



    /**
     * 个人私聊
     * @param userId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> like(
                @RequestParam(value = "userId") long userId,
                @RequestParam(value = "pageNum",defaultValue = "1",required = false)  int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Chat> chatList = chatDao.getChatList(userId, (pageNum - 1) * rows, rows);
            for (Chat chat:chatList){
                if (userId==chat.getMyId()&&userId!=chat.getToId()){
                    chat.setType(1);
                }else{
                    chat.setType(2);
                }
            }
            resultMap.put("chatList",chatList);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }
}
